#include "stdafx.h"
#include "IMultiply.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IMultiply::FunctionNameIMultiply = "Multiply";

IMultiply::IMultiply(void) 
{
	_fnApi.SetFunctionName(IMultiply::FunctionNameIMultiply);
	_fnApi.SetFunctionDescription("Multiplies a image on top");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Image", "Sets the Image to be Multiplied by. Make sure the dimensions are the same as that of the image you're using. Enter the path to the image", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(1, "Scale", "Sets the scale for the multiply function. Default = 1. Input value between 0 and 100", ProcParamDef::input, 0, 100));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IMultiply::~IMultiply(void)
{

}


bool IMultiply::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *source = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamDouble *Factor = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(1));
		if  (source != '\0'&& Factor != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());	
			cv::Mat input2 = cv::imread((std::string)source->GetValueStr());

			if (input.cols == input2.cols && input.rows == input2. rows)
			{
				cv::multiply(input, input2, output,double(Factor->GetValue()));

				cv::imwrite(step->GetIntermediateImagePathFilename(), output);
				
				input.release();
				input2.release();
				output.release();
				rc = true;
			}
		}
	}
	return rc;
}

